<?php

/**

 * @package     Joomla.Administrator

 * @subpackage  com_bricrop

 *

 * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.

 * @license     GNU General Public License version 2 or later; see LICENSE.txt

 */



// No direct access

defined('_JEXEC') or die;



jimport('joomla.application.component.view');



/**

 * View class for a list of regions.

 *

 * @package     Joomla.Administrator

 * @subpackage  com_bricrop

 * @since       1.6

 */

class RegionsViewCitys extends JView

{

	protected $items;

	protected $pagination;

	protected $state;



	/**

	 * Method to display the view.

	 *

	 * @param   string  $tpl  A template file to load. [optional]

	 *

	 * @return  mixed  A string if successful, otherwise a JError object.

	 *

	 * @since   1.6

	 */

	public function display($tpl = null)

	{

		// Initialise variables.
		$levellimit = 10;
		$rows		= $this->get('Items');
		$db = & JFactory::getDBO();

		$idb =$this->get('CountryId')? $this->get('CountryId') : 0;
		$children = array();
		// first pass - collect children
		//echo count($rows);
		foreach ($rows as $v )
		{
			$pt = $v->parent;
			$list = @$children[$pt] ? $children[$pt] : array();
			array_push( $list, $v );
			$children[$pt] = $list;
		}
		
		// second pass - get an indent list of the items
		//$this->items = JHTML::_('menu.treerecurse', $idb, '', array(), $children, max( 0, $levellimit-1 ) );
		$this->items = $rows;
		//echo count($this->items);

		$this->pagination	= $this->get('Pagination');

		$this->state		= $this->get('State');



		// Check for errors.

		if (count($errors = $this->get('Errors')))

		{

			JError::raiseError(500, implode("\n", $errors));

			return false;

		}

		$this->addToolbar();

		parent::display($tpl);

	}



	/**

	 * Add the page title and toolbar.

	 *

	 * @return  void

	 *

	 * @since   1.6

	 */

	protected function addToolbar()
	{

		require_once JPATH_COMPONENT.'/helpers/regions.php';

		$canDo = RegionsHelper::getActions();

		$user = JFactory::getUser();

		JToolBarHelper::title(JText::_('COM_REGIONS'));

		

		JToolBarHelper::addNew('city.add');



		if (($canDo->get('core.edit')))

		{

			JToolBarHelper::editList('city.edit');

		}



		if ($canDo->get('core.edit.state'))

		{

			if ($this->state->get('filter.state') != 2)

			{

				JToolBarHelper::publish('citys.publish', 'JTOOLBAR_PUBLISH', true);

				JToolBarHelper::unpublish('citys.unpublish', 'JTOOLBAR_UNPUBLISH', true);

			}



			if ($this->state->get('filter.state') != -1)

			{

				if ($this->state->get('filter.state') == 2)

				{

					JToolBarHelper::unarchiveList('citys.publish');

				}

			}

		}



		if ($canDo->get('core.delete'))

		{

			JToolBarHelper::deleteList('', 'citys.delete', 'JTOOLBAR_DELETE');

		}



		if ($canDo->get('core.admin'))

		{

			JToolBarHelper::preferences('com_regions');

		}

	}

}

